<template>
    <div>
        <!--新增栏目内容-->
            <div class="main-right">
                <dynamic-form :sendData="someData" v-model="formObj">
                </dynamic-form>
              </div>
            <el-row>
                <!--保存按钮-->
                  <el-button :span=1 class="grid">
                      <el-button type="success" @click = "add" icon="el-icon-plus" size="mini" >保存</el-button>
                  </el-button>
            </el-row>
    </div>
</template>
<script>
import Vue from 'vue'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/index.js'
Vue.use(Element)
import dynamicForm from './../../components/dynamic-form.vue'
import dynamicTable from './../../components/dynamic-table.vue'

export default {
  name: 'addContent',
  components:{
      dynamicForm, dynamicTable
    },
  data: function (){
      return {
       active:true,
       //表格数据
       someData: {
          templateId: "",
          formItemList:[]
        },
        formObj : []
      }
    },
  methods: {
//    $listeners(e) {
//      this.formObj ={
//          'name':1,
//          'sec':2
//      }
//    },
    add(){

        // console.log("this.$refs.child.formObj:" +JSON.stringify(this.$refs.child.formObj));
        // console.log(JSON.stringify(this.formObj));
        // this.formObj = this.$refs.child.formObj
        this.formObj = []
        this.someData.formItemList.forEach((item) => {
            var formItem = {
              templateId: this.someData.templateId,
              name : item.key,
              value : item.text
            }
            // console.log("formItem:" +JSON.stringify(formItem));
            this.formObj.push(formItem);
         })
        this.$http.post('http://127.0.0.1:8080/attrPreView/add',this.formObj).then(function(result){
                  var body = result.body
                  if (body.success === true) {
                     this.$router.push({name:"contentManager", params: {templateId: this.someData.templateId}});
                     return
                  }
                  },function(result){
                     alert(result.status)
                  });

     }
  },
  created (){//无法在这里面直接调用method中的方法
      // alert("this.$route.params.templateId:" + this.$route.params.templateId)
        this.$http.get('http://127.0.0.1:8080/attrPreView/'+this.$route.params.templateId).then(function(result){
            var body = result.body
            if (body.success === true) {

                if(!body.payload.formItemList){
                  return
                }
                this.someData.templateId = body.payload.templateId
                body.payload.formItemList.forEach((item) => {
                                      this.someData.formItemList.push(item);
                                   })
                console.log(JSON.stringify(this.someData))
            }
            },function(result){
               alert(result.status)
            });
   },
  render:{
  }
};
</script>
